package com.qiyadeng.jsonrpc;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.alexd.jsonrpc.JSONRPCClient;
import org.alexd.jsonrpc.JSONRPCException;
import org.alexd.jsonrpc.JSONRPCParams;
import org.apache.http.client.HttpClient;

import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;

public class LimeSurveyJsonRpc {

//	public static void main(String[] args) throws MalformedURLException {
//		URL serverUrl = new URL("http://demo.limesurvey.org/index.php/admin/remotecontrol");//get_session_key
//		JSONRPC2Session mySession = new JSONRPC2Session(serverUrl);
//		//String method="get_session_key";
//		String method="test";
//		
//		List  paramList = new LinkedList();
//		paramList.add("admin");
//		paramList.add("test");
//		Map parmMap = new HashMap();
//		parmMap.put("username", "admin");
//		parmMap.put("password", "test");
//		JSONRPC2Request request = new JSONRPC2Request(method, parmMap, "requ-id-1");
//		//JSONRPC2Request request = new JSONRPC2Request(method,"req-id-1");
//		mySession.getOptions().setRequestContentType("application/json");
//		//mySession.getOptions().setAllowedResponseContentTypes(new String[]{"application/json"});
//		System.out.println("request:"+request);
//		JSONRPC2Response response = null;
//		try {
//			response = mySession.send(request);
//		} catch (JSONRPC2SessionException e) {
//			e.printStackTrace();
//		}
//		if(response.indicatesSuccess()){
//			System.out.println(response.getResult());
//		}
//		else
//		{
//			//System.out.println(response.getError().getMessage());
//		}
//		
//	
//	}

public static void main(String[] args) {
	
	JSONRPCClient client = JSONRPCClient.create("http://demo.limesurvey.org/index.php/admin/remotecontrol/", JSONRPCParams.Versions.VERSION_2);
	client.setConnectionTimeout(2000);
	client.setSoTimeout(2000);
	client.setDebug(true);
	try 
	{
	  Object object = client.call("get_session_key","admin","test");
	  
	  System.out.println(object);
	  //double d = client.callDouble("pow", x, y);
	  //String i = client.callString("test", 56, 25);
	 // String string = client.callString("test", params);
	}
	catch (JSONRPCException e)
	{
	  e.printStackTrace();
	}
}

}
